package threading;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-06-16
 * Time: 17:32
 */
public class ThreadDemo25 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //只是创建了任务，还没有执行
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for(int i = 1; i <= 1000; i++) {
                    sum += i;
                }
                return sum;
            }
        };

        //还需要找个人来完成这个任务即线程
        //Thread 不能直接传callable 需要再包装一层

        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t = new Thread(futureTask);
        t.start();
        System.out.println(futureTask.get());
        //此处的get就是获取到上述任务call方法返回值的结果
        //调用get与join类似会阻塞等待，并获取到值
    }
}
